/**
 * Copyright (c) 2008, George Taylor
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted 
 * provided that the following conditions are met:
 * 
 *   * Redistributions of source code must retain the above copyright notice, this list of 
 *     conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright notice, this list of 
 *     conditions and the following disclaimer in the documentation and/or other materials 
 *     provided with the distribution.
 *   * Neither the name of the copyright holder's organization nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software without specific 
 *     prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package uk.co.taylorconsulting.annoweb.web.controller;

import java.math.BigDecimal;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.log4j.Logger;

import uk.co.taylorconsulting.annoweb.web.builder.ParameterRequiredException;

public abstract class WebHelper extends CacheHelper {
	private static HashMap<String, Object> cache = new HashMap<String, Object>();
	private static final Logger            LOG   = Logger.getLogger(WebHelper.class);
	
	protected String getDispatch(HttpServletRequest request, String dispatch) {
		String retValue = request.getParameter(dispatch);
		if (retValue == null) {
			try {
				retValue = (String) request.getAttribute(dispatch);
			} catch (Exception e) {}
		}
		return retValue;
	}
	
	protected Throwable getException(String value) {
		return null;
	}
	
	protected Throwable getException(String name, HttpServletRequest request, boolean required, String key, String bundle, Class<? extends Throwable> base, Exception defaultValue) throws ParameterRequiredException {
		try {
			Throwable t = getException(name, request, required, key, bundle, base);
			if (t == null)
				t = defaultValue;
			return t;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected Throwable getException(String name, HttpServletRequest request, boolean required, String key, String bundle, Class<? extends Throwable> base) throws ParameterRequiredException {
		Object o = null;
		o = getObject(name, request, required, key, bundle);
		if (o != null) {
			if (!(o instanceof Throwable)) {
				o = null;
			}
		}
		if (o != null) {
			if (!base.isAssignableFrom(o.getClass())) {
				o = null;
			}
		}
		return (Throwable) o;
	}
	
	protected String getString(String value) {
		return value;
	}
	
	protected String getString(String name, HttpServletRequest request, boolean required, String key, String bundle, String defaultValue) throws ParameterRequiredException {
		try {
			String s = getString(name, request, required, key, bundle);
			if (s == null)
				s = defaultValue;
			return s;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected String getString(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			return (String) getObject(name, request, required, key, bundle);
		} catch (Exception e) {}
		return null;
	}
	
	protected byte getPByte(String value) {
		try {
			return Byte.parseByte(value);
		} catch (Exception e) {}
		return -1;
	}
	
	protected byte getPByte(String name, HttpServletRequest request, boolean required, String key, String bundle, byte defaultValue) throws ParameterRequiredException {
		try {
			byte b = getPByte(name, request, required, key, bundle);
			return b;
		} catch (ParameterRequiredException pre) {
			return defaultValue;
		}
	}
	
	protected byte getPByte(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object o = getObject(name, request, required, key, bundle);
			if (o instanceof String) {
				return Byte.parseByte((String) o);
			} else if (o instanceof Byte) {
				return ((Byte) o);
			}
		} catch (Exception e) {}
		return -1;
	}
	
	protected Byte getByte(String value) {
		try {
			return Byte.parseByte(value);
		} catch (Exception e) {}
		return null;
	}
	
	protected Byte getByte(String name, HttpServletRequest request, boolean required, String key, String bundle, Byte defaultValue) throws ParameterRequiredException {
		try {
			Byte b = getByte(name, request, required, key, bundle);
			if (b == null)
				b = defaultValue;
			return b;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected Byte getByte(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		Object o = getObject(name, request, required, key, bundle);
		try {
			if (o instanceof Byte) {
				return (Byte) o;
			} else if (o instanceof String) {
				try {
					return Byte.parseByte((String) getObject(name, request, required, key, bundle));
				} catch (Exception e2) {}
			}
		} catch (Exception e) {}
		return null;
	}
	
	protected short getPShort(String value) {
		try {
			return Short.parseShort(value);
		} catch (Exception e) {}
		return -1;
	}
	
	protected short getPShort(String name, HttpServletRequest request, boolean required, String key, String bundle, short defaultValue) throws ParameterRequiredException {
		try {
			short s = getByte(name, request, required, key, bundle);
			return s;
		} catch (ParameterRequiredException pre) {
			return defaultValue;
		}
	}
	
	protected short getPShort(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object o = getObject(name, request, required, key, bundle);
			if (o instanceof String) {
				return Short.parseShort((String) o);
			} else if (o instanceof Short) {
				return ((Short) o);
			}
		} catch (Exception e) {}
		return -1;
	}
	
	protected Short getShort(String value) {
		try {
			return Short.parseShort(value);
		} catch (Exception e) {}
		return null;
	}
	
	protected Short getShort(String name, HttpServletRequest request, boolean required, String key, String bundle, Short defaultValue) throws ParameterRequiredException {
		try {
			Short s = getShort(name, request, required, key, bundle);
			if (s == null)
				s = defaultValue;
			return s;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected Short getShort(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		Object o = getObject(name, request, required, key, bundle);
		try {
			if (o instanceof Short) {
				return (Short) o;
			} else if (o instanceof String) {
				try {
					return Short.parseShort((String) getObject(name, request, required, key, bundle));
				} catch (Exception e2) {}
			}
		} catch (Exception e) {}
		return null;
	}
	
	protected int getInt(String value) {
		try {
			return Integer.parseInt(value);
		} catch (Exception e) {}
		return -1;
	}
	
	protected int getInt(String name, HttpServletRequest request, boolean required, String key, String bundle, int defaultValue) throws ParameterRequiredException {
		try {
			int i = getInt(name, request, required, key, bundle);
			return i;
		} catch (ParameterRequiredException pre) {
			return defaultValue;
		}
	}
	
	protected int getInt(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object o = getObject(name, request, required, key, bundle);
			if (o instanceof String) {
				return Integer.parseInt((String) o);
			} else if (o instanceof Integer) {
				return ((Integer) o);
			}
		} catch (Exception e) {}
		return -1;
	}
	
	protected Integer getInteger(String value) {
		try {
			return Integer.parseInt(value);
		} catch (Exception e) {}
		return null;
	}
	
	protected Integer getInteger(String name, HttpServletRequest request, boolean required, String key, String bundle, Integer defaultValue) throws ParameterRequiredException {
		try {
			Integer i = getInteger(name, request, required, key, bundle);
			if (i == null)
				i = defaultValue;
			return i;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected Integer getInteger(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		Object o = getObject(name, request, required, key, bundle);
		try {
			if (o instanceof Integer) {
				return (Integer) o;
			} else if (o instanceof String) {
				return Integer.parseInt((String) o);
			}
		} catch (Exception e) {}
		return null;
	}
	
	protected BigDecimal getBigDecimal(String value) {
		try {
			return BigDecimal.valueOf(Long.parseLong(value));
		} catch (Exception e) {}
		return null;
	}
	
	protected BigDecimal getBigDecimal(String name, HttpServletRequest request, boolean required, String key, String bundle, BigDecimal defaultValue) throws ParameterRequiredException {
		try {
			BigDecimal b = getBigDecimal(name, request, required, key, bundle);
			if (b == null)
				b = defaultValue;
			return b;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected BigDecimal getBigDecimal(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		Object o = getObject(name, request, required, key, bundle);
		try {
			if (o instanceof BigDecimal) {
				return (BigDecimal) o;
			} else if (o instanceof String) {
				return BigDecimal.valueOf(Double.parseDouble((String) o));
			}
		} catch (Exception e) {}
		return null;
	}
	
	protected long getPLong(String value) {
		try {
			return Long.parseLong(value);
		} catch (Exception e) {}
		return -1;
	}
	
	protected long getPLong(String name, HttpServletRequest request, boolean required, String key, String bundle, long defaultValue) throws ParameterRequiredException {
		try {
			long b = getPLong(name, request, required, key, bundle);
			return b;
		} catch (ParameterRequiredException pre) {
			return defaultValue;
		}
	}
	
	protected long getPLong(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object o = getObject(name, request, required, key, bundle);
			if (o instanceof String) {
				return Long.parseLong((String) o);
			} else if (o instanceof Long) {
				return ((Long) o);
			}
		} catch (Exception e) {}
		return -1;
	}
	
	protected Long getLong(String value) {
		try {
			return Long.parseLong(value);
		} catch (Exception e) {}
		return null;
	}
	
	protected Long getLong(String name, HttpServletRequest request, boolean required, String key, String bundle, Long defaultValue) throws ParameterRequiredException {
		try {
			Long l = getLong(name, request, required, key, bundle);
			if (l == null)
				l = defaultValue;
			return l;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected Long getLong(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		Object o = getObject(name, request, required, key, bundle);
		try {
			if (o instanceof Long) {
				return (Long) o;
			} else if (o instanceof String) {
				return Long.parseLong((String) o);
			}
		} catch (Exception e) {}
		return null;
	}
	
	protected float getPFloat(String value) {
		try {
			return Float.parseFloat(value);
		} catch (Exception e) {}
		return -1.0f;
	}
	
	protected float getPFloat(String name, HttpServletRequest request, boolean required, String key, String bundle, float defaultValue) throws ParameterRequiredException {
		try {
			float f = getPFloat(name, request, required, key, bundle);
			return f;
		} catch (ParameterRequiredException pre) {
			return defaultValue;
		}
	}
	
	protected float getPFloat(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object o = getObject(name, request, required, key, bundle);
			if (o instanceof String) {
				return Float.parseFloat((String) o);
			} else if (o instanceof Float) {
				return ((Float) o);
			}
		} catch (Exception e) {}
		return -1;
	}
	
	protected Float getFloat(String value) {
		try {
			return Float.parseFloat(value);
		} catch (Exception e) {}
		return null;
	}
	
	protected Float getFloat(String name, HttpServletRequest request, boolean required, String key, String bundle, Float defaultValue) throws ParameterRequiredException {
		try {
			Float f = getFloat(name, request, required, key, bundle);
			if (f == null)
				f = defaultValue;
			return f;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected Float getFloat(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		Object o = getObject(name, request, required, key, bundle);
		try {
			if (o instanceof Float) {
				return (Float) o;
			} else if (o instanceof Float) {
				return Float.parseFloat((String) o);
			}
		} catch (Exception e) {}
		return null;
	}
	
	protected double getPDouble(String value) {
		try {
			return Double.parseDouble(value);
		} catch (Exception e) {}
		return -1.0;
	}
	
	protected double getPDouble(String name, HttpServletRequest request, boolean required, String key, String bundle, double defaultValue) throws ParameterRequiredException {
		try {
			Double d = getDouble(name, request, required, key, bundle);
			if (d == null)
				d = defaultValue;
			return d;
		} catch (ParameterRequiredException pre) {
			return defaultValue;
		}
	}
	
	protected double getPDouble(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object o = getObject(name, request, required, key, bundle);
			if (o instanceof String) {
				return Double.parseDouble((String) o);
			} else if (o instanceof Double) {
				return ((Double) o);
			}
		} catch (Exception e) {}
		return -1;
	}
	
	protected Double getDouble(String value) {
		try {
			return Double.parseDouble(value);
		} catch (Exception e) {}
		return null;
	}
	
	protected Double getDouble(String name, HttpServletRequest request, boolean required, String key, String bundle, Double defaultValue) throws ParameterRequiredException {
		try {
			Double d = getDouble(name, request, required, key, bundle);
			if (d == null)
				d = defaultValue;
			return d;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected Double getDouble(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		Object o = getObject(name, request, required, key, bundle);
		try {
			if (o instanceof Double) {
				return (Double) o;
			} else if (o instanceof String) {
				return Double.parseDouble((String) o);
			}
		} catch (Exception e) {}
		return null;
	}
	
	protected boolean getPBoolean(String value) {
		try {
			return Boolean.parseBoolean(value);
		} catch (Exception e) {}
		return false;
	}
	
	protected boolean getPBoolean(String name, HttpServletRequest request, boolean required, String key, String bundle, boolean defaultValue) throws ParameterRequiredException {
		try {
			boolean b = getBoolean(name, request, required, key, bundle);
			return b;
		} catch (ParameterRequiredException pre) {
			return defaultValue;
		}
	}
	
	protected boolean getPBoolean(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object o = getObject(name, request, required, key, bundle);
			if (o instanceof String) {
				return Boolean.parseBoolean((String) o);
			} else if (o instanceof Boolean) {
				return ((Boolean) o);
			}
		} catch (Exception e) {}
		return Boolean.FALSE;
	}
	
	protected Boolean getBoolean(String value) {
		try {
			return Boolean.parseBoolean(value);
		} catch (Exception e) {}
		return null;
	}
	
	protected Boolean getBoolean(String name, HttpServletRequest request, boolean required, String key, String bundle, Boolean defaultValue) throws ParameterRequiredException {
		try {
			Boolean b = getBoolean(name, request, required, key, bundle);
			if (b == null)
				b = defaultValue;
			return b;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected Boolean getBoolean(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		Object o = getObject(name, request, required, key, bundle);
		try {
			if (o instanceof Boolean) {
				return (Boolean) o;
			} else if (o instanceof String) {
				return Boolean.parseBoolean((String) o);
			}
		} catch (Exception e) {}
		return null;
	}
	
	protected char getPChar(String value) {
		try {
			return value.charAt(0);
		} catch (Exception e) {}
		return Character.MIN_VALUE;
	}
	
	protected char getPChar(String name, HttpServletRequest request, boolean required, String key, String bundle, char defaultValue) throws ParameterRequiredException {
		try {
			char c = getPChar(name, request, required, key, bundle);
			return c;
		} catch (ParameterRequiredException pre) {
			return defaultValue;
		}
	}
	
	protected char getPChar(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object o = getObject(name, request, required, key, bundle);
			if (o instanceof String) {
				return ((String) o).charAt(0);
			} else if (o instanceof Character) {
				return ((Character) o);
			}
		} catch (Exception e) {}
		return ' ';
	}
	
	protected Character getCharacter(String value) {
		try {
			return value.charAt(0);
		} catch (Exception e) {}
		return null;
	}
	
	protected Character getCharacter(String name, HttpServletRequest request, boolean required, String key, String bundle, Character defaultValue) throws ParameterRequiredException {
		try {
			Character c = getCharacter(name, request, required, key, bundle);
			if (c == null)
				c = defaultValue;
			return c;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected Character getCharacter(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		Object o = getObject(name, request, required, key, bundle);
		try {
			if (o instanceof Character) {
				return (Character) o;
			} else if (o instanceof String) {
				return Character.valueOf(((String) o).charAt(0));
			}
		} catch (Exception e) {}
		return null;
	}
	
	protected Object getObject(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		Object s = request.getParameter(name);
		if (s == null) {
			s = request.getAttribute(name);
			if (s == null) {
				s = request.getSession().getAttribute(name);
			}
		}
		if (s == null) {
			if (required)
				throw new ParameterRequiredException("missing parameter " + name, key, bundle);
		}
		return s;
	}
	
	protected String[] getStringA(String value) {
		try {
			return value.split(",");
		} catch (Exception e) {}
		return null;
	}
	
	protected String[] getStringA(String name, HttpServletRequest request, boolean required, String key, String bundle, String[] defaultValue) throws ParameterRequiredException {
		try {
			String[] s = getStringA(name, request, required, key, bundle);
			if (s == null)
				s = defaultValue;
			return s;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected String[] getStringA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				return (String[]) o;
			}
		} catch (Exception e) {}
		return new String[0];
	}
	
	protected byte[] getPByteA(String value) {
		try {
			String[] ops = value.split(",");
			byte[] retValue = new byte[ops.length];
			for (int i = 0; i < ops.length; i++) {
				retValue[i] = getPByte(ops[i]);
			}
			return retValue;
		} catch (Exception e) {}
		return null;
	}
	
	protected byte[] getPByteA(String name, HttpServletRequest request, boolean required, String key, String bundle, byte[] defaultValue) throws ParameterRequiredException {
		try {
			byte[] b = getPByteA(name, request, required, key, bundle);
			if (b == null)
				b = defaultValue;
			return b;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected byte[] getPByteA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				byte[] retValue = new byte[o.length];
				int i = 0;
				for (String s : (String[]) o) {
					retValue[i++] = Byte.parseByte(s);
				}
				return retValue;
			} else if (o instanceof Byte[]) {
				byte[] retValue = new byte[o.length];
				int i = 0;
				for (Byte b : (Byte[]) o) {
					retValue[i++] = b;
				}
				return retValue;
			}
		} catch (Exception e) {}
		return new byte[0];
	}
	
	protected Byte[] getByteA(String value) {
		try {
			String[] ops = value.split(",");
			Byte[] retValue = new Byte[ops.length];
			for (int i = 0; i < ops.length; i++) {
				retValue[i] = getByte(ops[i]);
			}
			return retValue;
		} catch (Exception e) {}
		return null;
	}
	
	protected Byte[] getByteA(String name, HttpServletRequest request, boolean required, String key, String bundle, Byte[] defaultValue) throws ParameterRequiredException {
		try {
			Byte[] b = getByteA(name, request, required, key, bundle);
			if (b == null)
				b = defaultValue;
			return b;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected Byte[] getByteA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				Byte[] retValue = new Byte[o.length];
				int i = 0;
				for (String s : (String[]) o) {
					retValue[i++] = Byte.parseByte(s);
				}
				return retValue;
			} else if (o instanceof Byte[]) {
				return (Byte[]) o;
			}
		} catch (Exception e) {}
		return new Byte[0];
	}
	
	protected short[] getPShortA(String value) {
		try {
			String[] ops = value.split(",");
			short[] retValue = new short[ops.length];
			for (int i = 0; i < ops.length; i++) {
				retValue[i] = getPShort(ops[i]);
			}
			return retValue;
		} catch (Exception e) {}
		return null;
	}
	
	protected short[] getShortA(String name, HttpServletRequest request, boolean required, String key, String bundle, short[] defaultValue) throws ParameterRequiredException {
		try {
			short[] s = getPShortA(name, request, required, key, bundle);
			if (s == null)
				s = defaultValue;
			return s;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected short[] getPShortA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				short[] retValue = new short[o.length];
				int i = 0;
				for (String s : (String[]) o) {
					retValue[i++] = Short.parseShort(s);
				}
				return retValue;
			} else if (o instanceof Short[]) {
				short[] retValue = new short[o.length];
				int i = 0;
				for (Short b : (Short[]) o) {
					retValue[i++] = b;
				}
				return retValue;
			}
		} catch (Exception e) {}
		return new short[0];
	}
	
	protected Short[] getShortA(String value) {
		try {
			String[] ops = value.split(",");
			Short[] retValue = new Short[ops.length];
			for (int i = 0; i < ops.length; i++) {
				retValue[i] = getShort(ops[i]);
			}
			return retValue;
		} catch (Exception e) {}
		return null;
	}
	
	protected Short[] getShortA(String name, HttpServletRequest request, boolean required, String key, String bundle, Short[] defaultValue) throws ParameterRequiredException {
		try {
			Short[] s = getShortA(name, request, required, key, bundle);
			if (s == null)
				s = defaultValue;
			return s;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected Short[] getShortA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				Short[] retValue = new Short[o.length];
				int i = 0;
				for (String s : (String[]) o) {
					retValue[i++] = Short.parseShort(s);
				}
				return retValue;
			} else if (o instanceof Byte[]) {
				return (Short[]) o;
			}
		} catch (Exception e) {}
		return new Short[0];
	}
	
	protected int[] getIntA(String value) {
		try {
			String[] ops = value.split(",");
			int[] retValue = new int[ops.length];
			for (int i = 0; i < ops.length; i++) {
				retValue[i] = getInt(ops[i]);
			}
			return retValue;
		} catch (Exception e) {}
		return null;
	}
	
	protected int[] getIntA(String name, HttpServletRequest request, boolean required, String key, String bundle, int[] defaultValue) throws ParameterRequiredException {
		try {
			int[] i = getIntA(name, request, required, key, bundle);
			if (i == null)
				i = defaultValue;
			return i;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected int[] getIntA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				int[] retValue = new int[o.length];
				int i = 0;
				for (String s : (String[]) o) {
					retValue[i++] = Integer.parseInt(s);
				}
				return retValue;
			} else if (o instanceof Integer[]) {
				int[] retValue = new int[o.length];
				int i = 0;
				for (Integer b : (Integer[]) o) {
					retValue[i++] = b;
				}
				return retValue;
			}
		} catch (Exception e) {}
		return new int[0];
	}
	
	protected Integer[] getIntegerA(String value) {
		try {
			String[] ops = value.split(",");
			Integer[] retValue = new Integer[ops.length];
			for (int i = 0; i < ops.length; i++) {
				retValue[i] = getInteger(ops[i]);
			}
			return retValue;
		} catch (Exception e) {}
		return null;
	}
	
	protected Integer[] getIntegerA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				Integer[] retValue = new Integer[o.length];
				int i = 0;
				for (String s : (String[]) o) {
					retValue[i++] = Integer.parseInt(s);
				}
				return retValue;
			} else if (o instanceof Integer[]) {
				return (Integer[]) o;
			}
		} catch (Exception e) {}
		return new Integer[0];
	}
	
	protected FileItem getFileItem(String value) {
		return null;
	}
	
	protected FileItem getFileItem(String name, HttpServletRequest request, boolean required, String key, String bundle, FileItem defaultValue) throws ParameterRequiredException {
		try {
			FileItem f = getFileItem(name, request, required, key, bundle);
			if (f == null)
				f = defaultValue;
			return f;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected FileItem getFileItem(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		FileItem retValue = null;
		try {
			retValue = (FileItem) getObject(name, request, required, key, bundle);
		} catch (Exception e) {}
		return retValue;
	}
	
	protected FileItem[] getFileItemAA(String value) {
		return null;
	}
	
	protected FileItem[] getFileItemA(String name, HttpServletRequest request, boolean required, String key, String bundle, FileItem[] defaultValue) throws ParameterRequiredException {
		try {
			FileItem[] f = getFileItemA(name, request, required, key, bundle);
			if (f == null)
				f = defaultValue;
			return f;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected FileItem[] getFileItemA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		FileItem[] retValue = null;
		try {
			retValue = (FileItem[]) getObjectA(name, request, required, key, bundle);
		} catch (Exception e) {
			retValue = new FileItem[0];
		}
		return retValue;
	}
	
	protected BigDecimal[] getBigDecimalA(String value) {
		try {
			String[] ops = value.split(",");
			BigDecimal[] retValue = new BigDecimal[ops.length];
			for (int i = 0; i < ops.length; i++) {
				retValue[i] = getBigDecimal(ops[i]);
			}
			return retValue;
		} catch (Exception e) {}
		return null;
	}
	
	protected BigDecimal[] getBigDecimalA(String name, HttpServletRequest request, boolean required, String key, String bundle, BigDecimal[] defaultValue) throws ParameterRequiredException {
		try {
			BigDecimal[] b = getBigDecimalA(name, request, required, key, bundle);
			if (b == null)
				b = defaultValue;
			return b;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected BigDecimal[] getBigDecimalA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				BigDecimal[] retValue = new BigDecimal[o.length];
				int i = 0;
				for (String s : (String[]) o) {
					retValue[i++] = BigDecimal.valueOf(Double.parseDouble(s));
				}
				return retValue;
			} else if (o instanceof BigDecimal[]) {
				return (BigDecimal[]) o;
			}
		} catch (Exception e) {}
		return new BigDecimal[0];
	}
	
	protected long[] getPLongA(String value) {
		try {
			String[] ops = value.split(",");
			long[] retValue = new long[ops.length];
			for (int i = 0; i < ops.length; i++) {
				retValue[i] = getPLong(ops[i]);
			}
			return retValue;
		} catch (Exception e) {}
		return null;
	}
	
	protected long[] getPLongA(String name, HttpServletRequest request, boolean required, String key, String bundle, long[] defaultValue) throws ParameterRequiredException {
		try {
			long[] l = getPLongA(name, request, required, key, bundle);
			if (l == null)
				l = defaultValue;
			return l;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected long[] getPLongA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				long[] retValue = new long[o.length];
				int i = 0;
				for (String s : (String[]) o) {
					retValue[i++] = Long.parseLong(s);
				}
				return retValue;
			} else if (o instanceof Long[]) {
				long[] retValue = new long[o.length];
				int i = 0;
				for (Long b : (Long[]) o) {
					retValue[i++] = b;
				}
				return retValue;
			}
		} catch (Exception e) {}
		return new long[0];
	}
	
	protected Long[] getLongA(String value) {
		try {
			String[] ops = value.split(",");
			Long[] retValue = new Long[ops.length];
			for (int i = 0; i < ops.length; i++) {
				retValue[i] = getLong(ops[i]);
			}
			return retValue;
		} catch (Exception e) {}
		return null;
	}
	
	protected Long[] getLongA(String name, HttpServletRequest request, boolean required, String key, String bundle, Long[] defaultValue) throws ParameterRequiredException {
		try {
			Long[] l = getLongA(name, request, required, key, bundle);
			if (l == null)
				l = defaultValue;
			return l;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected Long[] getLongA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				Long[] retValue = new Long[o.length];
				int i = 0;
				for (String s : (String[]) o) {
					retValue[i++] = Long.parseLong(s);
				}
				return retValue;
			} else if (o instanceof Long[]) {
				return (Long[]) o;
			}
		} catch (Exception e) {}
		return new Long[0];
	}
	
	protected float[] getPFloatA(String value) {
		try {
			String[] ops = value.split(",");
			float[] retValue = new float[ops.length];
			for (int i = 0; i < ops.length; i++) {
				retValue[i] = getPFloat(ops[i]);
			}
			return retValue;
		} catch (Exception e) {}
		return null;
	}
	
	protected float[] getPLfloatA(String name, HttpServletRequest request, boolean required, String key, String bundle, float[] defaultValue) throws ParameterRequiredException {
		try {
			float[] f = getPFloatA(name, request, required, key, bundle);
			if (f == null)
				f = defaultValue;
			return f;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected float[] getPFloatA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				float[] retValue = new float[o.length];
				int i = 0;
				for (String s : (String[]) o) {
					retValue[i++] = Float.parseFloat(s);
				}
				return retValue;
			} else if (o instanceof Float[]) {
				float[] retValue = new float[o.length];
				int i = 0;
				for (Float b : (Float[]) o) {
					retValue[i++] = b;
				}
				return retValue;
			}
		} catch (Exception e) {}
		return new float[0];
	}
	
	protected Float[] getFloatA(String value) {
		try {
			String[] ops = value.split(",");
			Float[] retValue = new Float[ops.length];
			for (int i = 0; i < ops.length; i++) {
				retValue[i] = getFloat(ops[i]);
			}
			return retValue;
		} catch (Exception e) {}
		return null;
	}
	
	protected Float[] getPfloatA(String name, HttpServletRequest request, boolean required, String key, String bundle, Float[] defaultValue) throws ParameterRequiredException {
		try {
			Float[] f = getFloatA(name, request, required, key, bundle);
			if (f == null)
				f = defaultValue;
			return f;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected Float[] getFloatA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				Float[] retValue = new Float[o.length];
				int i = 0;
				for (String s : (String[]) o) {
					retValue[i++] = Float.parseFloat(s);
				}
				return retValue;
			} else if (o instanceof Float[]) {
				return (Float[]) o;
			}
		} catch (Exception e) {}
		return new Float[0];
	}
	
	protected double[] getPDoubleA(String value) {
		try {
			String[] ops = value.split(",");
			double[] retValue = new double[ops.length];
			for (int i = 0; i < ops.length; i++) {
				retValue[i] = getPDouble(ops[i]);
			}
			return retValue;
		} catch (Exception e) {}
		return null;
	}
	
	protected double[] getPDoubleA(String name, HttpServletRequest request, boolean required, String key, String bundle, double[] defaultValue) throws ParameterRequiredException {
		try {
			double[] d = getPDoubleA(name, request, required, key, bundle);
			if (d == null)
				d = defaultValue;
			return d;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected double[] getPDoubleA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				double[] retValue = new double[o.length];
				int i = 0;
				for (String s : (String[]) o) {
					retValue[i++] = Double.parseDouble(s);
				}
				return retValue;
			} else if (o instanceof Double[]) {
				double[] retValue = new double[o.length];
				int i = 0;
				for (Double b : (Double[]) o) {
					retValue[i++] = b;
				}
				return retValue;
			}
		} catch (Exception e) {}
		return new double[0];
	}
	
	protected Double[] getDoubleA(String value) {
		try {
			String[] ops = value.split(",");
			Double[] retValue = new Double[ops.length];
			for (int i = 0; i < ops.length; i++) {
				retValue[i] = getDouble(ops[i]);
			}
			return retValue;
		} catch (Exception e) {}
		return null;
	}
	
	protected Double[] getDoubleA(String name, HttpServletRequest request, boolean required, String key, String bundle, Double[] defaultValue) throws ParameterRequiredException {
		try {
			Double[] d = getDoubleA(name, request, required, key, bundle);
			if (d == null)
				d = defaultValue;
			return d;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected Double[] getDoubleA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				Double[] retValue = new Double[o.length];
				int i = 0;
				for (String s : (String[]) o) {
					retValue[i++] = Double.parseDouble(s);
				}
				return retValue;
			} else if (o instanceof Double[]) {
				return (Double[]) o;
			}
		} catch (Exception e) {}
		return new Double[0];
	}
	
	protected boolean[] getPBooleanA(String value) {
		try {
			String[] ops = value.split(",");
			boolean[] retValue = new boolean[ops.length];
			for (int i = 0; i < ops.length; i++) {
				retValue[i] = getPBoolean(ops[i]);
			}
			return retValue;
		} catch (Exception e) {}
		return null;
	}
	
	protected boolean[] getPBooleanA(String name, HttpServletRequest request, boolean required, String key, String bundle, boolean[] defaultValue) throws ParameterRequiredException {
		try {
			boolean[] b = getPBooleanA(name, request, required, key, bundle);
			if (b == null)
				b = defaultValue;
			return b;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected boolean[] getPBooleanA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				boolean[] retValue = new boolean[o.length];
				int i = 0;
				for (String s : (String[]) o) {
					retValue[i++] = Boolean.parseBoolean(s);
				}
				return retValue;
			} else if (o instanceof Boolean[]) {
				boolean[] retValue = new boolean[o.length];
				int i = 0;
				for (Boolean b : (Boolean[]) o) {
					retValue[i++] = b;
				}
				return retValue;
			}
		} catch (Exception e) {}
		return new boolean[0];
	}
	
	protected Boolean[] getBooleanA(String value) {
		try {
			String[] ops = value.split(",");
			Boolean[] retValue = new Boolean[ops.length];
			for (int i = 0; i < ops.length; i++) {
				retValue[i] = getBoolean(ops[i]);
			}
			return retValue;
		} catch (Exception e) {}
		return null;
	}
	
	protected Boolean[] getBooleanA(String name, HttpServletRequest request, boolean required, String key, String bundle, Boolean[] defaultValue) throws ParameterRequiredException {
		try {
			Boolean[] b = getBooleanA(name, request, required, key, bundle);
			if (b == null)
				b = defaultValue;
			return b;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected Boolean[] getBooleanA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				Boolean[] retValue = new Boolean[o.length];
				int i = 0;
				for (String s : (String[]) o) {
					retValue[i++] = Boolean.parseBoolean(s);
				}
				return retValue;
			} else if (o instanceof Boolean[]) {
				return (Boolean[]) o;
			}
		} catch (Exception e) {}
		return null;
	}
	
	protected char[] getPCharA(String value) {
		try {
			String[] ops = value.split(",");
			char[] retValue = new char[ops.length];
			for (int i = 0; i < ops.length; i++) {
				retValue[i] = getPChar(ops[i]);
			}
			return retValue;
		} catch (Exception e) {}
		return null;
	}
	
	protected char[] getPCharA(String name, HttpServletRequest request, boolean required, String key, String bundle, char[] defaultValue) throws ParameterRequiredException {
		try {
			char[] c = getPCharA(name, request, required, key, bundle);
			if (c == null)
				c = defaultValue;
			return c;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected char[] getPCharA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				char[] retValue = new char[o.length];
				int i = 0;
				for (String s : (String[]) o) {
					retValue[i++] = s.charAt(0);
				}
				return retValue;
			} else if (o instanceof Character[]) {
				char[] retValue = new char[o.length];
				int i = 0;
				for (Character b : (Character[]) o) {
					retValue[i++] = b;
				}
				return retValue;
			}
		} catch (Exception e) {}
		return new char[0];
	}
	
	protected Character[] getCharacterA(String value) {
		try {
			String[] ops = value.split(",");
			Character[] retValue = new Character[ops.length];
			for (int i = 0; i < ops.length; i++) {
				retValue[i] = getCharacter(ops[i]);
			}
			return retValue;
		} catch (Exception e) {}
		return null;
	}
	
	protected Character[] getCharacterA(String name, HttpServletRequest request, boolean required, String key, String bundle, Character[] defaultValue) throws ParameterRequiredException {
		try {
			Character[] c = getCharacterA(name, request, required, key, bundle);
			if (c == null)
				c = defaultValue;
			return c;
		} catch (ParameterRequiredException pre) {
			if (defaultValue == null)
				throw pre;
			return defaultValue;
		}
	}
	
	protected Character[] getCharacterA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		try {
			Object[] o = getObjectA(name, request, required, key, bundle);
			if (o instanceof String[]) {
				Character[] retValue = new Character[o.length];
				int i = 0;
				for (String s : (String[]) o) {
					retValue[i++] = s.charAt(0);
				}
				return retValue;
			} else if (o instanceof Character[]) {
				return (Character[]) o;
			}
		} catch (Exception e) {}
		return new Character[0];
	}
	
	protected Object[] getObjectA(String name, HttpServletRequest request, boolean required, String key, String bundle) throws ParameterRequiredException {
		Object[] s = request.getParameterValues(name);
		if (s == null || s.length < 1) {
			s = (Object[]) request.getAttribute(name);
			if (s == null) {
				s = (Object[]) request.getSession().getAttribute(name);
			}
		}
		if (s == null) {
			if (required)
				throw new ParameterRequiredException("missing parameter " + name, key, bundle);
		}
		return s;
	}
	
	public Object getCache(String key) {
		Object o = cache.get(key);
		if (o == null) {
			try {
				o = Class.forName(key).newInstance();
				cache.put(key, o);
			} catch (Exception e) {
				LOG.warn("Error creating class from key " + key + ":" + e.getLocalizedMessage());
			}
		}
		return o;
	}
}
